package com.commerceE.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.commerceE.common.exception.ServiceException;
import com.commerceE.dao.ImageDao;
import com.commerceE.domain.Images;
import com.commerceE.service.ImageService;
import com.commerceE.util.Constant;

public class ImageServiceImpl implements ImageService {

	@Autowired
	private ImageDao imageDao;

	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public void insertImage(Images image) throws ServiceException {
		imageDao.insert(image);
	}

	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public void updateImage(Images image) throws ServiceException {
		imageDao.update(image);
	}

	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public void deleteImage(Long imageId) throws ServiceException {
		imageDao.delete(imageId);
	}
	
	@Transactional(readOnly = true, rollbackFor = Exception.class)
	public List<Images> getImagesByInstanceId(Long instanceId){
		return imageDao.getImagesByInstanceId(instanceId);
	}
	
	public Images getProductSmallImage(Long instanceId){
		List<Images> imageList = imageDao.getImagesByInstanceIdAndImageType(instanceId, Constant.IMAGE_TYPE_PRODUCT_SMALL);
		if(imageList!=null && imageList.size()!=0){
			return imageList.get(0);
		}else{
			return null;
		}
	}
	
	public Images getProductBestBuyImage(Long instanceId){
		List<Images> imageList = imageDao.getImagesByInstanceIdAndImageType(instanceId, Constant.IMAGE_TYPE_PRODUCT_BESTBUY);
		if(imageList!=null && imageList.size()!=0){
			return imageList.get(0);
		}else{
			return null;
		}
	}
	
	public List<Images> getProductRegularImage(Long instanceId){
		return  imageDao.getImagesByInstanceIdAndImageType(instanceId, Constant.IMAGE_TYPE_PRODUCT_REGULAR);
	}
	
	public List<Images> getImagesByInstanceIdAndImageType(Long instanceId, String imageType){
		return imageDao.getImagesByInstanceIdAndImageType(instanceId, imageType);
	}

}
